// pages/newyiyuan/index.js
const app = getApp()
const DB = wx.cloud.database();
let len = "";
let len2 = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    type: '',
    // top标签显示（默认不显示）
    backTopValue: false,
    orderItem: [],
    orderItem2: [],

    openid: "",
    userPhoto: "",
    nickName: " ",
    logged: "false",
    disabled: true,
    fenleitype: ['全部','hpv疫苗',"新冠疫苗", "轮状疫苗", "狂犬疫苗", '流感疫苗', "乙肝疫苗", "肺炎疫苗"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  onChange(event) {
    this.setData({ active: event.detail });
  },

    onChangea(e) {
      this.setData({
          title:e.detail.title
      })
        if (e.detail.title === '全部') {
            DB.collection('yimiao').where({
                    zhanghao: wx.getStorageSync('user').zhanghao,
                    mima: wx.getStorageSync('user').mima,
                })
                .get()
                .then((res) => {
                    console.log(res)
                    this.setData({
                        orderItem2: res.data,
                    })
                })

        } else {
            DB.collection('yimiao').where({
                    zhanghao: wx.getStorageSync('user').zhanghao,
                    mima: wx.getStorageSync('user').mima,
                    yimiaotype: e.detail.title
                })
                .get()
                .then((res) => {
                    console.log(res)
                    this.setData({
                        orderItem2: res.data,
                    })
                })
        }

    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const res = wx.getSystemInfoSync()
    var statusbarH = res.statusBarHeight
    console.log(statusbarH);
    this.setData({
        statusbar :statusbarH, // 状态栏高度
        jiaonangheight: wx.getMenuButtonBoundingClientRect().bottom+10  // 胶囊高度
    })
    
    console.log(wx.getMenuButtonBoundingClientRect(), '胶囊属性');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    len2 = this.data.orderItem2.length
    DB.collection('yimiao').where({
     zhanghao:wx.getStorageSync('user').zhanghao,
     mima:wx.getStorageSync('user').mima,
    }).skip(len).orderBy('time_order', 'desc').get().then(res => {
      console.log("b", res)
      wx.hideLoading();
      this.setData({
        orderItem2: res.data
      })
      console.log("this", this.data.orderItem2)
    })

    DB.collection('news').where({
      zhanghao: wx.getStorageSync('user').zhanghao,
      mima: wx.getStorageSync('user').mima,
  }).skip(len).orderBy('time_order', 'desc').get().then(res => {
      console.log("b", res)
      wx.hideLoading();
      this.setData({
          orderItem3: res.data
      })

  })

  DB.collection('dingdan').where({
    yiyuanuserid:wx.getStorageSync('user')._id
    }).get().then(res => {
        console.log(res);
        this.setData({
            list: res.data
        })
    })


  this.setData({
    openid:wx.getStorageSync('openid')
  })
  DB.collection('users').where({
    zhanghao: wx.getStorageSync('user').zhanghao,
    mima: wx.getStorageSync('user').mima,
  }).get().then((res) => {
    console.log(res);
    if (res.data.length) {
      app.userInfo = Object.assign(app.userInfo, res.data[0]);
      this.setData({
        userPhoto: app.userInfo.userPhoto,
        nickName: app.userInfo.nickName,
        logged: true,
      })
    } else {
      this.setData({
        disabled: false
      })
    }
  })



  },
  goDetailb(e){
    wx.navigateTo({
      url: '../dingdandetail/index?id='+e.currentTarget.dataset.id,
    })
 },
  goDetail(e) {
    wx.navigateTo({
      url: '../yimiaodetail/index?id=' + e.currentTarget.dataset.id,
    })
  },
  shanchu(e) {
    let that = this;
    wx.showModal({
        title: '温馨提示',
        content: '是否删除？',
        confirmText: "是",
        cancelText: "否",
        success(res) {
            if (res.confirm) {
                wx.showLoading({
                    title: '删除中',
                })
                wx.cloud.callFunction({
                    name: "update",
                    data: {
                        action: "shanchuxw",
                        id: e.currentTarget.dataset.id,
                    }
                }).then(res => {
                    wx.hideLoading({
                        success: (res) => {},
                    })

                    DB.collection('news').where({
                        zhanghao: wx.getStorageSync('user').zhanghao,
                        mima: wx.getStorageSync('user').mima,

                    }).orderBy('time_order', 'desc').get().then(res => {

                        wx.hideLoading();
                        that.setData({
                            orderItem3: res.data
                        })
                    })

                })
            } else if (res.cancel) {}
        }
    })
},
goDetaila(e) {
  wx.navigateTo({
      url: '../newsdetail/index?id=' + e.currentTarget.dataset.id,
  })
},
goDetailf(e) {
  wx.navigateTo({
      url: '../dingdandetail/index?id=' + e.currentTarget.dataset.id,
  })
},
tuichu() {
  wx.setStorageSync('user', '')
  wx.redirectTo({
    url: '../shouye/index',
  })
}
})